﻿using System;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WPFGame
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            LoadExamples();
        }

        private void LoadExamples()
        {
            string windowsPath = Environment.CurrentDirectory.Substring
                (0, Environment.CurrentDirectory.LastIndexOf("bin")) + "Windows";

            //MessageBox.Show(windowsPath);
            if (Directory.Exists(windowsPath))
            {
               string[] examples = Directory.GetFiles(windowsPath,"*.xaml");

               foreach (string s in examples)
               {
                   string name = s.Substring(s.LastIndexOf("\\") + 1).Replace(".xaml", "");
                   exampleTreeView.Items.Add(name);
               }

               exampleTreeView.MouseDoubleClick += new MouseButtonEventHandler(exampleTreeView_MouseDoubleClick);
            }
        }

        void exampleTreeView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (exampleTreeView.SelectedItem == null) return;

            //Assembly asm = Assembly.ReflectionOnlyLoad("WPFGame");
            Assembly asm = Assembly.GetExecutingAssembly();
            Type t = asm.GetType("WPFGame.Windows." + exampleTreeView.SelectedItem.ToString());
            object obj = Activator.CreateInstance(t);
            UserControl uc = (UserControl)obj;
            contentGrid.Children.Clear();
            contentGrid.Children.Add(uc);
            tbExampleTitle.Text = uc.Tag == null ? "未命名" : uc.Tag.ToString();
        }
    }
}
